package com.turbo.cloud.ums.utils;


import jakarta.servlet.http.HttpServletRequest;

public class HappyCaptchaRequestUtil {
    /**
     * 登录请求失败SESSION失败
     */
    public final static String REQUEST_SESSION_LOGIN_FAIL_KEY = "ums-login-fail-count";
    /**
     * 数字3
     */
    public final static Integer NUM_THREE = 3;

    /**
     * 保存用户登录失败次数
     */
    public static boolean showHappyCaptcha(HttpServletRequest request) {
        int loginFailCount = getLoginFailCount(request);
        return loginFailCount >= NUM_THREE;
    }

    /**
     * 保存用户登录失败次数
     */
    public static void setLoginFailCount(HttpServletRequest request) {
        int loginFailCount = getLoginFailCount(request) + 1;
        request.getSession().setAttribute(REQUEST_SESSION_LOGIN_FAIL_KEY, loginFailCount);
    }

    /**
     * 获取用户登录失败次数
     */
    public static int getLoginFailCount(HttpServletRequest request) {
        if (request.getSession().getAttribute(REQUEST_SESSION_LOGIN_FAIL_KEY) != null) {
            return (int) request.getSession().getAttribute(REQUEST_SESSION_LOGIN_FAIL_KEY);
        }
        return 0;
    }

    /**
     * 删除用户登录失败次数
     */
    public static void removeLoginFailCount(HttpServletRequest request) {
        request.getSession().removeAttribute(REQUEST_SESSION_LOGIN_FAIL_KEY);
    }
}
